#!/usr/bin/env python
# -*- coding: utf-8 -*-

# This project is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# 
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This project is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Tutoo Installer.  If not, see <http://www.gnu.org/licenses/>.

from PyQt4 import QtGui, QtCore
import os

size = {'width': 70, 'height':63}
fixed = 20

class Generic_Object():
	def __init__(self, main_window, name=None, pixmap=None):
		self.name = name
		self.main_window = main_window
		self.pos = {'x':160, 'y':110}

		self.start_links = []
		self.end_links = []

		#Configuration about processor pixmap
		rect = QtCore.QRect(self.pos['x'], self.pos['y'], size['width'], size['height'])
		self.pixmap = QtGui.QLabel(self.main_window.centralwidget)
		self.pixmap.setGeometry(rect)
		self.pixmap.setObjectName(self.name)
		self.pixmap.setText(self.name)
		self.pixmap.setScaledContents(True)
		self.pixmap.setPixmap(QtGui.QPixmap(pixmap))
		self.pixmap.show()

	def move(self, move_x, move_y):
		self.pos['x'] = self.pos['x'] + move_x
		self.pos['y'] = self.pos['y'] + move_y
		rect = QtCore.QRect(self.pos['x'], self.pos['y'], size['width'], size['height'])

		self.pixmap.setGeometry(rect)

	def get_pos(self):
		return self.pos

	def inside(self, x, y):
		success = x > self.pos['x']
		success = success and y > self.pos['y'] + fixed
		success = success and x < self.pos['x'] + size['width']
		success = success and y < self.pos['y'] + size['height'] + fixed
		return success

	def get_center(self):
		x = self.pos['x'] + (size['width']/2)
		y = self.pos['y'] + (size['height']) - fixed/2

		return {'x':x, 'y':y}

	def start_link(self, obj):
		self.start_links.append(obj)

	def end_link(self, obj):
		self.end_links.append(obj)

	def remove_start_link(self, obj):
		self.start_links.remove(obj)

	def remove_end_link(self, obj):
		self.end_links.remove(obj)

	def get_start_links(self):
		return self.start_links

	def get_end_links(self):
		return self.end_links

	def get_name(self):
		return self.name

	def set_name(self, name):
		self.name = name

	def set_selected(self):
		lines = [ QtCore.QLine(self.pos['x'], self.pos['y'] + fixed, self.pos['x'] + size['width'], self.pos['y'] + fixed),
			QtCore.QLine(self.pos['x'] + size['width'], self.pos['y'] + fixed, self.pos['x'] + size['width'], self.pos['y'] + size['height'] + fixed),
			QtCore.QLine(self.pos['x'] + size['width'], self.pos['y'] + size['height'] + fixed, self.pos['x'], self.pos['y'] + size['height'] + fixed),
			QtCore.QLine(self.pos['x'], self.pos['y'] + size['height'] + fixed, self.pos['x'], self.pos['y'] + fixed), ]
		return lines

	def get_type(self):
		if hasattr(self, 'obj_type'):
			return self.obj_type
		return None

	def set_type(self, obj_type):
		self.obj_type = obj_type

	def set_pixmap(self, pixmap):
		self.pixmap.setPixmap(QtGui.QPixmap(pixmap))

	def destroy(self):
		self.pixmap.hide()
